package com.example.dataView.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dataView.common.result.R;
import com.example.dataView.pojo.SchoolInfo;
import com.example.dataView.mapper.SchoolInfoMapper;
import com.example.dataView.service.SchoolInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 成大事
 * @since 2022-06-09 20:26:57
 */
@Service
public class SchoolInfoServiceImpl extends ServiceImpl<SchoolInfoMapper, SchoolInfo> implements SchoolInfoService {

    @Resource
    private SchoolInfoMapper schoolInfoMapper;

    /**
     * 中国地图各个省份的高校的数量
     * @return  R
     */
    @Override
    public R<Object> getProvinceSchoolNum() {
        List<Map<String, Object>> maps = schoolInfoMapper.selectMaps(new QueryWrapper<SchoolInfo>()
                .select("id","count(1) as value", "province as name")
                .groupBy("province")
                .orderByDesc("value"));
        if(maps != null){
            return R.ok(maps);
        }
        return R.error();
    }


    /**
     * 根据省份获取这个省份的高校
     * @param province 省份id
     * @return  R
     */
    @Override
    public R<Object> getSchoolNumByProvinceId(String province) {
        try {
            List<Map<String, Object>> maps = schoolInfoMapper.selectMaps(new QueryWrapper<SchoolInfo>()
                    .eq("province", province));
            return R.ok(maps);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }


}
